package com.android.tripspins.app;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;


import com.android.tripspins.app.adapter.TripListAdapter;
import com.android.tripspins.app.bo.Trip;
import com.android.tripspins.app.bo.User;
import com.android.tripspins.app.dao.TripDAO;
import com.android.tripspins.app.dao.UserDAO;
import com.android.tripspins.app.helper.ConstantsHelper;
import com.android.tripspins.app.helper.DbHelper;
import com.android.tripspins.app.helper.MenuHelper;
import com.android.tripspins.app.services.BackgroundPinGeneratorService;
import com.android.tripspins.app.services.BackgroundSyncService;

import java.util.ArrayList;


public class ListTripsActivity extends FragmentActivity implements SharedPreferences.OnSharedPreferenceChangeListener{

    TripDAO tripDao;
    UserDAO userDao;
    ArrayList<Trip> trips;
    boolean isOnTheRoad;
    boolean isAuthenticated;
    ListView listTrips;
    DbHelper dbHelper;
    SharedPreferences settings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_trips);

        dbHelper = new DbHelper(this);
        loadTripsData();

        MenuHelper.loadMenu(this, isOnTheRoad, isAuthenticated);
        settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        /*Intent backgroundSyncServiceIntent = new Intent(this, BackgroundSyncService.class);
        startService(backgroundSyncServiceIntent);*/

        //Start service for pin generation
        if(isOnTheRoad) {
            Intent backgroundPinGeneratorService = new Intent(this, BackgroundPinGeneratorService.class);
            startService(backgroundPinGeneratorService);
        }
    }
    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void loadTripsData()
    {
        SQLiteDatabase database =  dbHelper.getWritableDatabase();
        userDao = new UserDAO(database);
        tripDao = new TripDAO(database);
        User loggedUser = userDao.getLoggedUser();
        isOnTheRoad = tripDao.isOnTheRoad();
        isAuthenticated = loggedUser != null;
        if(isAuthenticated) {
            trips = tripDao.getTripsByUser(loggedUser.getId());
        } else {
            trips = tripDao.getAnonymousTrips();
        }
        Trip[] tripsArray = new Trip[trips.size()];
        for(int i = 0; i< trips.size(); i++){ tripsArray[i] = trips.get(i); }
        database.close();
        TripListAdapter adapter = new TripListAdapter(this,tripsArray);
        listTrips = (ListView)findViewById(R.id.list_trips);
        listTrips.setAdapter(adapter);
    }

    @Override
    protected void onRestart() {
        super.onStart();
        loadTripsData();
        invalidateOptionsMenu();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        menu.clear();
        if(isOnTheRoad)
            getMenuInflater().inflate(R.menu.menu_on_the_road, menu);
        else
            getMenuInflater().inflate(R.menu.menu_off_the_road, menu);

        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.clear();
        if(isOnTheRoad)
            getMenuInflater().inflate(R.menu.menu_on_the_road, menu);
        else
            getMenuInflater().inflate(R.menu.menu_off_the_road, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        switch (item.getItemId()) {
            case R.id.menu_new_trip:
                final Intent intentNewTripActivity = new Intent(this,NewPinActivity.class);
                intentNewTripActivity.putExtra("param", ConstantsHelper.PIN_START);
                startActivity(intentNewTripActivity );
                return true;
            case R.id.menu_new_pin:
                final Intent intentNewPinActivity = new Intent(this,NewPinActivity.class);
                intentNewPinActivity.putExtra("param", ConstantsHelper.PIN_DEFAULT);
                startActivity(intentNewPinActivity );
                return true;

            case R.id.menu_settings:
                final Intent intentSettingsActivity = new Intent(this,SettingsActivity.class);
                startActivity(intentSettingsActivity);
                return true;

            case R.id.menu_finish:
                final Intent intentFinishTripActivity = new Intent(this,NewPinActivity.class);
                intentFinishTripActivity.putExtra("param", ConstantsHelper.PIN_FINISH);
                startActivity(intentFinishTripActivity );
                return true;
            case R.id.menu_login_fb:
                final Intent intentLoginActivity = new Intent(this,LoginActivity.class);
                startActivity(intentLoginActivity);
                return true;

            default:
                DrawerLayout leftMenu = (DrawerLayout)findViewById(R.id.drawer_layout);
                ListView listMenu = (ListView)findViewById(R.id.left_drawer);
                if(leftMenu.isDrawerOpen(listMenu))
                    leftMenu.closeDrawer(listMenu);
                else
                    leftMenu.openDrawer(listMenu);
                return true;
        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        /*if(key.equals(ConstantsHelper.PREFERENCES_BOOL_MONITOR))
        {
//
//            Preference connectionPref = findPreference(key);
            //kill process
        }*/

    }
}
